package com.chat.datea.fragment;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.chat.datea.R;
import com.chat.datea.adapter.RanKingAdapter;
import com.chat.datea.base.BaseFragment;
import com.chat.datea.base.Finnal_Base;
import com.chat.datea.bean.RanKingEntity;
import com.chat.datea.utils.volley.VolleySingleton;
import com.google.gson.Gson;

/**
 * Created by dllo on 15/10/20.
 */
public class MusicRankingFragment extends BaseFragment {
    private ListView listView;
    private RanKingAdapter kingAdapter;
    private VolleySingleton singleton;
    private String Url = Finnal_Base.apiUrl + Finnal_Base.method + "baidu.ting.billboard.billCategory" + Finnal_Base.format + "&kflag=1";
    private RanKingEntity.ContentEntity content;
    private ProgressBar progressBar;

    @Override
    protected void initData() {
        singleton = VolleySingleton.getVolleySingleton(mActivity);
        listView.setEmptyView(progressBar);
        getData();
    }

    @Override
    protected void initView() {
        listView = (ListView) view.findViewById(R.id.ranKing_ListView);
        progressBar = (ProgressBar) view.findViewById(R.id.progress);
    }

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container) {
        view = inflater.inflate(R.layout.ranking_lsitview, null);
        return view;
    }

    /***
     * 解析数据
     **/
    public void getData() {
        StringRequest request = new StringRequest(Url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                try {
                    RanKingEntity entity = gson.fromJson(response, RanKingEntity.class);
                    //设置组件
                    kingAdapter = new RanKingAdapter(getActivity(), entity);
                    listView.setAdapter(kingAdapter);
                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            content = new RanKingEntity.ContentEntity();
                            content = (RanKingEntity.ContentEntity) parent.getItemAtPosition(position);
                            FragmentManager manager = getActivity().getSupportFragmentManager();
                            FragmentTransaction transaction = manager.beginTransaction();
                            transaction.replace(R.id.fl_content, new BillFragment(content.getType(), content.getPic_s192()));
                            transaction.addToBackStack("BillFragment");
                            transaction.commit();
                        }
                    });
                } catch (Exception e) {
                    Toast.makeText(mActivity, "接口出错", Toast.LENGTH_SHORT).show();

                }


            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        singleton.addQueue(request, "tag");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        singleton.removeRequest("tag");
    }
}
